package com.corpgovernment.organization.dataloader.db;

import com.corpgovernment.organization.dto.QueryExpressAddressDTO;
import com.corpgovernment.organization.entity.db.MbExpressAddress;
import com.corpgovernment.organization.mapper.MbExpressAddressMapper;
import com.ctrip.corp.obt.generic.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import tk.mybatis.mapper.entity.Example;
import tk.mybatis.mapper.entity.Example.Criteria;

import java.util.List;

@Component
public class MbExpressAddressLoader {

	@Autowired
	private MbExpressAddressMapper expressAddressMapper;

	public List<MbExpressAddress> queryByRecord(QueryExpressAddressDTO queryParam) {
		Example example = new Example(MbExpressAddress.class);
		Criteria criteria = example.createCriteria();
		criteria.andIn("orgId", queryParam.getOrgIds());
		if (StringUtils.isNotBlank(queryParam.getUid())) {
			criteria.andEqualTo("uid", queryParam.getUid());
		}
		if (queryParam.getIsCorporation() != null) {
			criteria.andEqualTo("isCorporation", queryParam.getIsCorporation());
		}
		criteria.andEqualTo("isDeleted", false);
		String selectValue = queryParam.getSelectValue();
		if (selectValue != null) {
			Criteria selectValCriteria = example.createCriteria();
			selectValue = "%" + selectValue + "%";
			selectValCriteria.orLike("provinceName", selectValue);
			selectValCriteria.orLike("cityName", selectValue);
			selectValCriteria.orLike("districtName", selectValue);
			selectValCriteria.orLike("address", selectValue);
			selectValCriteria.orLike("recipientName", selectValue);
			selectValCriteria.orLike("recipientPhone", selectValue);
			example.and(selectValCriteria);
		}
		example.orderBy("datachangeLasttime").desc();
		return expressAddressMapper.selectByExample(example);
	}
}
